<?php
/**
 * Created by PhpStorm.
 * User: chenyu
 * Date: 2017-08-02
 * Time: 16:50
 */

namespace yii\helpers;

use Yii;
use yii\helpers\Json;
use yii\i18n\Formatter;

class FormatterAll extends Formatter
{
    const  nullReturn = null ;
    const  nullBinary = 000000 ;
    const  binary_32  = 'qx*';

     public static  function asJson($value){
          if(empty($value)){
               return self::nullReturn ;
          }
         return Json::encode($value);
     }

     public static function asJsonp($value){
            if(empty($value)){
                return self::nullReturn ;
            }
            if(!is_string($value)){
                $value = (string) $value ;
            }
            if(!filter_var($value,FILTER_VALIDATE_URL,FILTER_FLAG_PATH_REQUIRED )){
                $value = Yii::$app->request->hostInfo .$value;
            }
            return $value ;
     }

     public static function asBinary($value,$formatBit = self::binary_32 ){
            if(empty($value)){
                return self::nullBinary ;
            }
            if(function_exists('pack')){
              return   pack($formatBit,$value);
            }
            // binary after to modify
            return ((int)$value)/2 ;
     }


}